package controllers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"new/kpg/app"
	"new/kpg/e"
	"new/models"
	"new/services"
)

//查找学生信息
func GetOut(c *gin.Context) {
	err, info, total := services.GetALLOut() //找到services层对应函数
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}

//删除学生信息
func DelOut(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	out_id := com.StrTo(m["out_id"]).MustInt()

	if out_id == -1 {
		app.INFO(c, 30001, "参数错误")
		return
	}
	err := services.DelOut(out_id)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "删除成功")
}

func UpdateOut(c *gin.Context) {
	b, _ := c.GetRawData() //获取原始字节
	var m map[string]string
	_ = json.Unmarshal(b, &m) //将结构类型转换为json字符串，以下同理
	if m["Out_id"] == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	out_id := m["out_id"]
	out_date := m["out_date"]
	ship_id := m["ship_id"]
	out_num := m["out_num"]
	out_ton := m["out_ton"]
	if out_id == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.UpdateOut(models.Out{out_id, out_date, ship_id, out_num, out_ton})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "更新成功")
}

func AddOut(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	Outout_id := m["Outout_id"]
	Outout_date := m["Outout_date"]
	Outship_id := m["Outship_id"]
	Outout_num := m["Outout_num"]
	Outout_ton := m["Outout_ton"]
	if Outout_id == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.AddOut(models.Out{Outout_id, Outout_date, Outship_id, Outout_num, Outout_ton})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "添加成功")
}
